﻿@using Mrc.Application;
@{
    ViewData["Title"] = "LogList";
    Layout = "~/Areas/Admin/Views/_Layout.cshtml";
}

<link href="~/lib/layui/layui_ext/dtree/dtree.css" rel="stylesheet" />
<link href="~/lib/layui/layui_ext/dtree/font/dtreefont.css" rel="stylesheet" />

<table class="layui-hide" id="tabledata" lay-filter="tabledata"></table>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        @Html.Raw(Mrc.Application.PermissionButtonHelper.CreateButtonHtml(new List<string> { "system.role.add", "system.role.update", "system.role.delete" }, ViewContext.HttpContext));
        <button class="layui-btn layui-btn-sm" lay-event="updaterole">配置权限</button>
        <button class="layui-btn layui-btn-sm" onclick="javascript:location.replace(location.href);">刷新</button>
    </div>
</script>

<script type="text/html" id="switchTpl">
    <!-- 这里的 checked 的状态只是演示 -->
    <input type="checkbox" name="sex" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="updaterolestatus" {{ d.isEnabled == true ? 'checked' : '' }}>
</script>

<script>
    layui.extend({
        dtree: '/lib/layui/layui_ext/dtree/dtree'
    }).use(['element', 'layer', 'dtree', 'form', 'table'], function () {
        var element = layui.element,
            layer = layui.layer,
            dtree = layui.dtree,
            form = layui.form,
            table = layui.table,
            $ = layui.$;
            table.render({
                    elem: '#tabledata'
                    , height: 'full-20'
                    , url: '/admin/role/models'
                    , toolbar: '#toolbarDemo'
                    , title: 'IP过滤'
                    , cols: [[
                        { type: 'checkbox', fixed: 'left' }
                        , { field: 'name', title: '角色名称', fixed: 'left', unresize: true, sort: true }
                        , { field: 'isEnable', title: '是否启用', width: 350, templet: switchTpl, unresize: true }
                        , { field: 'description', title: '描述' }
                    ]]
                    , limits: [10, 15, 40, 60, 80, 100, 200, 400]
                    , limit: 100 //默认采用10
                    , page: true
                    , request: {
                        pageName: 'pageNumber' //页码的参数名称，默认：page
                        , limitName: 'pageSize' //每页数据量的参数名，默认：limit
                    }
                });

                //头工具栏事件
        table.on('toolbar(tabledata)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'add':
                            x_admin_show("添加角色", "/admin/role/add", 600, 420);
                            break;
                        case 'update':
                            var data = checkStatus.data;
                            if (data.length > 1) {
                                layer.msg('请选择单个编辑项');
                                break;
                            }
                            else {

                                var thisline = data[0];
                                console.table(thisline);
                                var id = thisline.id;
                                console.log(id)
                                x_admin_show("修改角色", "/admin/role/update?Id=" + id, 600, 420);
                            }
                            break;
                        case 'delete':
                            var data = checkStatus.data;
                            if (data.length > 1) {
                                layer.msg('请选择删除项');
                                break;
                            }
                            else
                            {
                                var ids = [];
                                for (var i = 0; i < data.length; i++) {
                                    ids.push(data[i].id);
                                }
                                console.table(ids);
                                BaseAjax(ajaxType.post, "/admin/role/delete", { ids: ids }, function (repo) {
                                    if (repo.status == ajaxStatus.OK) {
                                        table.reload('tabledata', { page: { curr: 1 } });
                                    }
                                });
                            }
                            break;
                        case 'updaterole':
                            var data = checkStatus.data;
                            if (data.length > 1) {
                                layer.msg('请选择单个编辑项');
                                break;
                            }
                            else{

                                var thisline = data[0];
                                console.table(thisline);
                                var id = thisline.id;
                                layer.open({
                                    type: 1,
                                    title: "设置权限",
                                    area: ["500px", "80%"],
                                    content: '<ul id="selectParent" class="dtree" data-id="0"></ul>',
                                    btn: ['确认设置'],
                                    success: function (layero, index)
                                    {
                                        var DTree = dtree.render({
                                            obj: $(layero).find("#selectParent"),
                                            url: "/admin/role/GetRolePermissions?id=" + id,
                                            initLevel:5,
                                            checkbar: true // 开启复选框
                                        });
                                    },
                                    yes: function (index, layero)
                                    {
                                        var flag = true;
                                        var params = dtree.getCheckbarNodesParam("selectParent"); // 获取选中值
                                        if (params.length == 0) {
                                            layer.msg("请至少选择一个节点", { icon: 2 });
                                            flag = false;
                                        }
                                        var ids = [], names = [];
                                        for (var key in params)
                                        {
                                            var param = params[key];
                                            ids.push(param.nodeId);
                                        }
                                        BaseAjax(ajaxType.post, "/admin/role/SetPermission", { id: id, permissions:ids.join(",") }, function (repo) {
                                            if (flag)
                                            {
                                                layer.close(index);
                                            }
                                        });
                                    }
                                });
                            }
                            break;                          
                    };
        });
        //监听性别操作
        form.on('switch(updaterolestatus)', function (obj) {
            BaseAjax(ajaxType.post, "/admin/role/UpdateStatus", { id: this.value, isEnable: obj.elem.checked }, function (repo) {
                layer.msg(repo.msg);
            });
        });
    })
</script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use('table', function () {
        var table = layui.table;
        
    });
</script>

